Augment Golo in Go
1. Add a new predefined function
Create a new golang file in the predefined folder, for example notifications.go, with the following content:
package predefined
import (
"fmt"
"golo/object"
)
func Information(args ...object.Object) object.Object {
fmt.Println("[INFO]", args[0].Inspect())
return nil
}
- The function must accept a variable number of
object.Objectarguments and return anobject.Object.- You can use
args[0].Inspect()to get the string representation of the first argument.- Return
nilif the function does not need to return a value.- Always use the
predefinedpackage for predefined functions.
2. Register the new function in predefined/registered.functions.go
In the predefined/registered.functions.go file, add your new function to the PredefinedFunctions map:
map[string]*object.Builtin{
// ... other predefined functions ...
"information": {
Fn: Information,
},
// ... other predefined functions ...
}
3. Rebuild the Golo binary
go build
4. Use your new predefined function in Golo
main.golo:
module demo.main
function main = |args| {
information("this is a test message")
}
Then, run:
golo main.golo
The output should be:
[INFO] this is a test message